const fs = require('fs')
const join = require('path').join;
const { exit } = require('process');

const argv = process.argv
if (argv.length <= 2) {
    console.log('usage: node htmlSimpleJK.js srcPath dstPath')
    exit
}
let srcPath = argv[2] !== undefined ? argv[2] : "html"
let dstPath = argv[3] !== undefined ? argv[3] : "html_simple"
console.log(srcPath, dstPath);

// let srcPath = "html";
// let dstPath = "md";
try{
  fs.mkdirSync(dstPath);
}catch (error){
}

let files = fs.readdirSync(srcPath);
files.forEach(function (item, index) {
    let fPath = join(srcPath,item);
    console.log(fPath, item, index);

    let stat = fs.statSync(fPath);
    if (stat.isFile() === true &&  -1 !== item.indexOf(".html")) { 
      let md = simpleHtml(fPath);
      let dstFile = join(dstPath, item).replace(".html", ".html");
      fs.writeFileSync(dstFile, md, 'utf8' );
    }
});

function simpleHtml(file){
  let content = fs.readFileSync(file, 'utf8' );
  content = content.replace(/src="data:.*?;base64.*?"/g, "");
  content = content.replace(/<base href=".*?">/g, "");
  content = content.replace(/data-savepage-src/g, "src");
  content = content.replace(/"\/9j.*?"/g, "");
  content = content.replace(/<span class="iconfont"><\/span>复制代码/g, "");
  content = content.replace(/<svg.*?>.*?<\/svg>/g, "");
  content = content.replace(/<script.*?>(.|\n)*?<\/script>/g, "");
  content = content.replace(/<div class="Wz6esVdU_0">.*?<\/div><\/div><\/div>/g, "");
  content = content.replace(/<img src="https.*? class="_1-ZfmNK8_0">/g, "");
  content = content.replace(/<img src="http.*? class="_2273kGdT_0">/g, "<img src=\"images/blank.png\" class=\"_2273kGdT_0\">");
  content = content.replace(/<\/div><\/td><\/tr>/g, "</div></td></tr>\n");
  return content;
}









